<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ajax请求并渲染网络数据</title>
</head>
<body>
    <button>渲染</button>
    <ul class="list">
        <!-- <li>
            <img src="" alt="">
            <p></p>
        </li> -->
    </ul>
    


    <script>

        var btn = document.querySelector('button');
        var box = document.querySelector('.list');

        btn.onclick = () =>{
            // 创建、配置、发送
            var xhr = new XMLHttpRequest();
            xhr.open("GET","./02_json.php");
            xhr.send();

            // 处理响应
            xhr.onreadystatechange = () =>{
                // ajax状态码&&http状态码
                if(xhr.readyState === 4 && /^2\d{2}$/.test(xhr.status)){
                    // json字符串转为js对象
                    var obj = JSON.parse(xhr.responseText);
                    var html = '';
                    for(var i=0;i<obj.length;i++){
                        html += `
                            <li>
                                <img src="${obj[i].img}" alt="">
                                <p>${obj[i].title}</p>
                            </li>
                        `;
                    }
                    box.innerHTML = html;
                }
            }
        }
        
    </script>
</body>
</html>